package ru.shop.dropbox;

import play.Play;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.WebAuthSession;

/**
 * Intended to keep dropbox clients.
 * 
 * @author flo
 * 
 */
public final class Dropbox {	
	public final static DropboxAPI<WebAuthSession> client = new DropboxAPI<WebAuthSession>(generateSession("1"));
	
	/**
	 * Generate session for predefined dropbox key and secret. 
	 * 
	 * @param name Postfix name
	 * @return
	 */
	private static WebAuthSession generateSession(String name) {
		String key = Play.configuration.getProperty("shop.dropbox.key" + name);
		String secret = Play.configuration.getProperty("shop.dropbox.secret" + name);
		AppKeyPair appKey = new AppKeyPair(key, secret);
		
		String accessTokenKey = Play.configuration.getProperty("shop.dropbox.accessToken.key" + name);
		String accessTokenSecret = Play.configuration.getProperty("shop.dropbox.accessToken.secret" + name);
		AccessTokenPair accessToken = new AccessTokenPair(accessTokenKey, accessTokenSecret);
		
		WebAuthSession session = new WebAuthSession(appKey, WebAuthSession.AccessType.APP_FOLDER);
		session.setAccessTokenPair(accessToken);
		return session;
	}
}
